package saucaca.fragment;

import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.Util.TagName;
import saucaca.fragment.SelectPlaylistDialogFragment.GetPlaylist;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class LocalListFragment extends Fragment {
	String TAG = LocalListFragment.class.getSimpleName();
	MainActivity mActivity;
	ViewPager viewPager;
	OfflineTabPageAdapter TabAdapter;
	String strKeyList;
	private PagerSlidingTabStrip tabs;
	
	public static LocalListFragment newInstance(String SongId) {
		LocalListFragment f = new LocalListFragment();
		Bundle args = new Bundle();
		args.putString(MainActivity.ID, SongId);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.viewpager_tabstrip, container, false);
		findViewById(view);
		TabAdapter = new OfflineTabPageAdapter(getChildFragmentManager(), this);
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin);
		viewPager.setAdapter(TabAdapter);
		viewPager.setCurrentItem(0);
		tabs.setViewPager(viewPager);
		if (getArguments() != null) {
			Bundle b = getArguments();
			strKeyList = b.getString(MainActivity.ID);
			setPageshow(strKeyList);
		}
		mActivity.myActionBar.setMenu(R.string.menu_nhaccuatoi, false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden){
			mActivity.myActionBar.setMenu(R.string.menu_nhaccuatoi, false, false);
		}
	}
	public void setPageshow(String strKeyList) {
		if (strKeyList.equals(LocalAllMusic.ALBUM_MUSIC)) {
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_ALBUMS);
		} else if (strKeyList.equals(LocalAllMusic.PLAYLIST_MUSIC)) {
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_PLAYLIST);
		} else if (strKeyList.equals(LocalAllMusic.ARTIST_MUSIC)) {
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_ARTISTS);
		} else if (strKeyList.equals(LocalAllMusic.FILE_MUSIC)) {
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_FOLDER);
		} else if (strKeyList.equals(LocalAllMusic.VIDEO_MUSIC)) {
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_VIDEO);
		} else
			viewPager.setCurrentItem(OfflineTabPageAdapter.PAGE_SONGS);
	}

	private void findViewById(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	public class OfflineTabPageAdapter extends FragmentStatePagerAdapter {
		public static final int PAGE_PLAYLIST = 0;
		public static final int PAGE_SONGS = 1;
		public static final int PAGE_ALBUMS = 2;
		public static final int PAGE_ARTISTS = 3;
		public static final int PAGE_VIDEO = 4;
		public static final int PAGE_FOLDER = 5;
		LocalListFragment localListFragment;

		public OfflineTabPageAdapter(FragmentManager fm, LocalListFragment localListFragment) {
			super(fm);
			this.localListFragment = localListFragment;
		}

		String pageTitle[] = { LocalAllMusic.PLAYLIST_MUSIC, LocalAllMusic.SONGS_MUSIC, LocalAllMusic.ALBUM_MUSIC, LocalAllMusic.ARTIST_MUSIC, LocalAllMusic.VIDEO_MUSIC , LocalAllMusic.FILE_MUSIC };
		String pageTitle1[] = { "Playlist", "Bài hát", "Album", "Ca sĩ", "Video", "Folder" };

		@Override
		public CharSequence getPageTitle(int position) {
			return pageTitle1[position];
		}

		@Override
		public int getCount() {
			return pageTitle.length;
		}

		@Override
		public Fragment getItem(int i) {
			return LocalSongList.newInstance(pageTitle[i]);
		}
	}

}
